
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>async 函数 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="callback&&callback如果存在回调函数就执行.html" />
    
    
    <link rel="prev" href="Array.prototype.slice.call.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../cmd命令.html">
            
                <a href="../cmd命令.html">
            
                    
                    cmd命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Array.prototype.slice.call.html">
            
                <a href="Array.prototype.slice.call.html">
            
                    
                    Array.prototype.slice.call解析
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4" data-path="async 函数.html">
            
                <a href="async 函数.html">
            
                    
                    async 函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="callback&&callback如果存在回调函数就执行.html">
            
                <a href="callback&&callback如果存在回调函数就执行.html">
            
                    
                    callback&&callback() 如果存在回调函数就执行！
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="CSS3 nth-child使用注意.html">
            
                <a href="CSS3 nth-child使用注意.html">
            
                    
                    CSS3 nth-child(n)使用注意
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="css3的 -webkit-min-device-pixel-ratio属性 解决高清的适配.html">
            
                <a href="css3的 -webkit-min-device-pixel-ratio属性 解决高清的适配.html">
            
                    
                    css3的 -webkit-min-device-pixel-ratio属性 解决高清的适配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="CSS文本超出自动缩小字体.html">
            
                <a href="CSS文本超出自动缩小字体.html">
            
                    
                    CSS文本超出自动缩小字体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="element-ui默认样式修改.html">
            
                <a href="element-ui默认样式修改.html">
            
                    
                    element-ui默认样式修改
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="JavaScript setInterval函数详解.html">
            
                <a href="JavaScript setInterval函数详解.html">
            
                    
                    JavaScript setInterval() 函数详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="JavaScript概念之screenclientoffsetscrollinneravail的widthleft.html">
            
                <a href="JavaScript概念之screenclientoffsetscrollinneravail的widthleft.html">
            
                    
                    JavaScript概念之screenclientoffsetscrollinneravail的widthleft
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="js获取星期几.html">
            
                <a href="js获取星期几.html">
            
                    
                    js获取星期几
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="js监听滚动条事件和获取浏览器及屏幕高度和宽度等信息的方法.html">
            
                <a href="js监听滚动条事件和获取浏览器及屏幕高度和宽度等信息的方法.html">
            
                    
                    js监听滚动条事件和获取浏览器及屏幕高度和宽度等信息的方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="js时间戳与日期格式之间相互转换.html">
            
                <a href="js时间戳与日期格式之间相互转换.html">
            
                    
                    js时间戳与日期格式之间相互转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="JS数组reduce方法详解及高级技巧.html">
            
                <a href="JS数组reduce方法详解及高级技巧.html">
            
                    
                    JS数组reduce()方法详解及高级技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="js中获取时间new Date详细介绍.html">
            
                <a href="js中获取时间new Date详细介绍.html">
            
                    
                    js中获取时间new Date()详细介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="less传参问题：在url中插入变量今天在需要用less给url传递参数时遇到了小问题，一番搜索后有了下述解决方法.html">
            
                <a href="less传参问题：在url中插入变量今天在需要用less给url传递参数时遇到了小问题，一番搜索后有了下述解决方法.html">
            
                    
                    less传参问题：在url中插入变量今天在需要用less给url传递参数时遇到了小问题，一番搜索后有了下述解决方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="moment 和date-fns.html">
            
                <a href="moment 和date-fns.html">
            
                    
                    moment 和date-fns
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="node.JS中配置http-server.html">
            
                <a href="node.JS中配置http-server.html">
            
                    
                    node.JS中配置http-server
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="Object.keys详解.html">
            
                <a href="Object.keys详解.html">
            
                    
                    Object.keys详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="router-link各种属性解释.html">
            
                <a href="router-link各种属性解释.html">
            
                    
                    router-link各种属性解释
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="stylus使用介绍.html">
            
                <a href="stylus使用介绍.html">
            
                    
                    stylus使用介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="TypeError Cannot read property 'compilation' of undefined.html">
            
                <a href="TypeError Cannot read property 'compilation' of undefined.html">
            
                    
                    TypeError Cannot read property 'compilation' of undefined
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="Vue  keep-alive.html">
            
                <a href="Vue  keep-alive.html">
            
                    
                    Vue  keep-alive
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.25" data-path="Vue - 过渡与动画transition.html">
            
                <a href="Vue - 过渡与动画transition.html">
            
                    
                    Vue - 过渡与动画transition
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.26" data-path="vue-axios使用方法.html">
            
                <a href="vue-axios使用方法.html">
            
                    
                    vue-axios使用方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.27" data-path="vue-calendar-component.html">
            
                <a href="vue-calendar-component.html">
            
                    
                    vue-calendar-component
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.28" data-path="vue-infinite-loading.html">
            
                <a href="vue-infinite-loading.html">
            
                    
                    vue-infinite-loading
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.29" data-path="vue报错There are multiple modules with names that only differ in casing.html">
            
                <a href="vue报错There are multiple modules with names that only differ in casing.html">
            
                    
                    vue报错There are multiple modules with names that only differ in casing
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.30" data-path="Vue中注意target和currentTarget的使用.html">
            
                <a href="Vue中注意target和currentTarget的使用.html">
            
                    
                    Vue中注意target和currentTarget的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.31" data-path="关于给javascript对象添加、删除、修改对象的属性.html">
            
                <a href="关于给javascript对象添加、删除、修改对象的属性.html">
            
                    
                    关于给javascript对象添加、删除、修改对象的属性
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.32" data-path="获取当前地址坐标.html">
            
                <a href="获取当前地址坐标.html">
            
                    
                    获取当前地址坐标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.33" data-path="解决ajax跨域问题【5种解决方案】.html">
            
                <a href="解决ajax跨域问题【5种解决方案】.html">
            
                    
                    解决ajax跨域问题【5种解决方案】
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.34" data-path="解决vue-cli webpack打包后加载资源的路径问题.html">
            
                <a href="解决vue-cli webpack打包后加载资源的路径问题.html">
            
                    
                    解决vue-cli webpack打包后加载资源的路径问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.35" data-path="时间格式转换：201971转换为2019-7-1.html">
            
                <a href="时间格式转换：201971转换为2019-7-1.html">
            
                    
                    时间格式转换：201971转换为2019-7-1
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.36" data-path="数据库密码改了.html">
            
                <a href="数据库密码改了.html">
            
                    
                    数据库密码改了
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.37" data-path="语法getBoundingClientRect.html">
            
                <a href="语法getBoundingClientRect.html">
            
                    
                    语法getBoundingClientRect()
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.38" data-path="在js中使用length报错Error in renderTypeErrorcannot read property bgImg of null.html">
            
                <a href="在js中使用length报错Error in renderTypeErrorcannot read property bgImg of null.html">
            
                    
                    在js中使用length报错Error in renderTypeErrorcannot read property bgImg of null
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >async 函数</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="async-&#x51FD;&#x6570;"><strong>async &#x51FD;&#x6570;</strong></h1>
<p><strong>&#x4E00;&#x3001;&#x7EC8;&#x6781;&#x89E3;&#x51B3;</strong></p>
<p>&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x662F; JavaScript &#x7F16;&#x7A0B;&#x7684;&#x9EBB;&#x70E6;&#x4E8B;&#xFF0C;&#x9EBB;&#x70E6;&#x5230;&#x4E00;&#x76F4;&#x6709;&#x4EBA;&#x63D0;&#x51FA;&#x5404;&#x79CD;&#x5404;&#x6837;&#x7684;&#x65B9;&#x6848;&#xFF0C;&#x8BD5;&#x56FE;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x3002;</p>
<p>&#x4ECE;&#x6700;&#x65E9;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x5230; Promise &#x5BF9;&#x8C61;&#xFF0C;&#x518D;&#x5230; Generator &#x51FD;&#x6570;&#xFF0C;&#x6BCF;&#x6B21;&#x90FD;&#x6709;&#x6240;&#x6539;&#x8FDB;&#xFF0C;&#x4F46;&#x53C8;&#x8BA9;&#x4EBA;&#x89C9;&#x5F97;&#x4E0D;&#x5F7B;&#x5E95;&#x3002;&#x5B83;&#x4EEC;&#x90FD;&#x6709;&#x989D;&#x5916;&#x7684;&#x590D;&#x6742;&#x6027;&#xFF0C;&#x90FD;&#x9700;&#x8981;&#x7406;&#x89E3;&#x62BD;&#x8C61;&#x7684;&#x5E95;&#x5C42;&#x8FD0;&#x884C;&#x673A;&#x5236;&#x3002;</p>
<p>&#x5F02;&#x6B65;I/O&#x4E0D;&#x5C31;&#x662F;&#x8BFB;&#x53D6;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x5417;&#xFF0C;&#x5E72;&#x561B;&#x8981;&#x641E;&#x5F97;&#x8FD9;&#x4E48;&#x590D;&#x6742;&#xFF1F;&#x5F02;&#x6B65;&#x7F16;&#x7A0B;&#x7684;&#x6700;&#x9AD8;&#x5883;&#x754C;&#xFF0C;&#x5C31;&#x662F;&#x6839;&#x672C;&#x4E0D;&#x7528;&#x5173;&#x5FC3;&#x5B83;&#x662F;&#x4E0D;&#x662F;&#x5F02;&#x6B65;&#x3002;</p>
<p>async &#x51FD;&#x6570;&#x5C31;&#x662F;&#x96A7;&#x9053;&#x5C3D;&#x5934;&#x7684;&#x4EAE;&#x5149;&#xFF0C;&#x5F88;&#x591A;&#x4EBA;&#x8BA4;&#x4E3A;&#x5B83;&#x662F;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x7684;&#x7EC8;&#x6781;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x3002;</p>
<p><strong>&#x4E8C;&#x3001;async &#x51FD;&#x6570;&#x662F;&#x4EC0;&#x4E48;&#xFF1F;</strong></p>
<p>&#x4E00;&#x53E5;&#x8BDD;&#xFF0C;async &#x51FD;&#x6570;&#x5C31;&#x662F; Generator &#x51FD;&#x6570;&#x7684;&#x8BED;&#x6CD5;&#x7CD6;&#x3002;</p>
<p>&#x524D;&#x6587;&#x6709;&#x4E00;&#x4E2A; Generator &#x51FD;&#x6570;&#xFF0C;&#x4F9D;&#x6B21;&#x8BFB;&#x53D6;&#x4E24;&#x4E2A;&#x6587;&#x4EF6;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`var` `fs = require(``&apos;fs&apos;``);``var` `readFile = ``function` `(fileName){`` ``return` `new` `Promise(``function` `(resolve, reject){``  ``fs.readFile(fileName, ``function``(error, data){``   ``if` `(error) reject(error);``   ``resolve(data);``  ``});`` ``});``};``var` `gen = ``function``* (){`` ``var` `f1 = yield readFile(``&apos;/etc/fstab&apos;``);`` ``var` `f2 = yield readFile(``&apos;/etc/shells&apos;``);`` ``console.log(f1.toString());`` ``console.log(f2.toString());``};`
</code></pre><p>&#x5199;&#x6210; async &#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x662F;&#x4E0B;&#x9762;&#x8FD9;&#x6837;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`var` `asyncReadFile = async ``function` `(){`` ``var` `f1 = await readFile(``&apos;/etc/fstab&apos;``);`` ``var` `f2 = await readFile(``&apos;/etc/shells&apos;``);`` ``console.log(f1.toString());`` ``console.log(f2.toString());``};`
</code></pre><p>&#x4E00;&#x6BD4;&#x8F83;&#x5C31;&#x4F1A;&#x53D1;&#x73B0;&#xFF0C;async &#x51FD;&#x6570;&#x5C31;&#x662F;&#x5C06; Generator &#x51FD;&#x6570;&#x7684;&#x661F;&#x53F7;&#xFF08;*&#xFF09;&#x66FF;&#x6362;&#x6210; async&#xFF0C;&#x5C06; yield &#x66FF;&#x6362;&#x6210; await&#xFF0C;&#x4EC5;&#x6B64;&#x800C;&#x5DF2;&#x3002;</p>
<h2 id="&#x4E09;&#x3001;async-&#x51FD;&#x6570;&#x7684;&#x4F18;&#x70B9;"><strong>&#x4E09;&#x3001;async &#x51FD;&#x6570;&#x7684;&#x4F18;&#x70B9;</strong></h2>
<p>async &#x51FD;&#x6570;&#x5BF9; Generator &#x51FD;&#x6570;&#x7684;&#x6539;&#x8FDB;&#xFF0C;&#x4F53;&#x73B0;&#x5728;&#x4EE5;&#x4E0B;&#x4E09;&#x70B9;&#x3002;</p>
<p>&#xFF08;1&#xFF09;&#x5185;&#x7F6E;&#x6267;&#x884C;&#x5668;&#x3002; Generator &#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#x5FC5;&#x987B;&#x9760;&#x6267;&#x884C;&#x5668;&#xFF0C;&#x6240;&#x4EE5;&#x624D;&#x6709;&#x4E86; co &#x51FD;&#x6570;&#x5E93;&#xFF0C;&#x800C; async &#x51FD;&#x6570;&#x81EA;&#x5E26;&#x6267;&#x884C;&#x5668;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;async &#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#xFF0C;&#x4E0E;&#x666E;&#x901A;&#x51FD;&#x6570;&#x4E00;&#x6A21;&#x4E00;&#x6837;&#xFF0C;&#x53EA;&#x8981;&#x4E00;&#x884C;&#x3002;</p>
<pre><code>var result = asyncReadFile();
</code></pre><p>&#xFF08;2&#xFF09;&#x66F4;&#x597D;&#x7684;&#x8BED;&#x4E49;&#x3002; async &#x548C; await&#xFF0C;&#x6BD4;&#x8D77;&#x661F;&#x53F7;&#x548C; yield&#xFF0C;&#x8BED;&#x4E49;&#x66F4;&#x6E05;&#x695A;&#x4E86;&#x3002;async &#x8868;&#x793A;&#x51FD;&#x6570;&#x91CC;&#x6709;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#xFF0C;await &#x8868;&#x793A;&#x7D27;&#x8DDF;&#x5728;&#x540E;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x7ED3;&#x679C;&#x3002;</p>
<p>&#xFF08;3&#xFF09;&#x66F4;&#x5E7F;&#x7684;&#x9002;&#x7528;&#x6027;&#x3002; co &#x51FD;&#x6570;&#x5E93;&#x7EA6;&#x5B9A;&#xFF0C;yield &#x547D;&#x4EE4;&#x540E;&#x9762;&#x53EA;&#x80FD;&#x662F; Thunk &#x51FD;&#x6570;&#x6216; Promise &#x5BF9;&#x8C61;&#xFF0C;&#x800C; async &#x51FD;&#x6570;&#x7684; await &#x547D;&#x4EE4;&#x540E;&#x9762;&#xFF0C;&#x53EF;&#x4EE5;&#x8DDF; Promise &#x5BF9;&#x8C61;&#x548C;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x7684;&#x503C;&#xFF08;&#x6570;&#x503C;&#x3001;&#x5B57;&#x7B26;&#x4E32;&#x548C;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x4F46;&#x8FD9;&#x65F6;&#x7B49;&#x540C;&#x4E8E;&#x540C;&#x6B65;&#x64CD;&#x4F5C;&#xFF09;&#x3002;</p>
<h2 id="&#x56DB;&#x3001;async-&#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0;"><strong>&#x56DB;&#x3001;async &#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0;</strong></h2>
<p>async &#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x5C31;&#x662F;&#x5C06; Generator &#x51FD;&#x6570;&#x548C;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x5668;&#xFF0C;&#x5305;&#x88C5;&#x5728;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x91CC;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`async ``function` `fn(args){`` ``// ...``}``// &#x7B49;&#x540C;&#x4E8E;``function` `fn(args){ `` ``return` `spawn(``function``*() {``  ``// ...`` ``}); ``}`
</code></pre><p>&#x6240;&#x6709;&#x7684; async &#x51FD;&#x6570;&#x90FD;&#x53EF;&#x4EE5;&#x5199;&#x6210;&#x4E0A;&#x9762;&#x7684;&#x7B2C;&#x4E8C;&#x79CD;&#x5F62;&#x5F0F;&#xFF0C;&#x5176;&#x4E2D;&#x7684; spawn &#x51FD;&#x6570;&#x5C31;&#x662F;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x5668;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7ED9;&#x51FA; spawn &#x51FD;&#x6570;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x57FA;&#x672C;&#x5C31;&#x662F;&#x524D;&#x6587;&#x81EA;&#x52A8;&#x6267;&#x884C;&#x5668;&#x7684;&#x7FFB;&#x7248;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`function` `spawn(genF) {`` ``return` `new` `Promise(``function``(resolve, reject) {``  ``var` `gen = genF();``  ``function` `step(nextF) {``   ``try` `{``    ``var` `next = nextF();``   ``} ``catch``(e) {``    ``return` `reject(e); ``   ``}``   ``if``(next.done) {``    ``return` `resolve(next.value);``   ``} ``   ``Promise.resolve(next.value).then(``function``(v) {``    ``step(``function``() { ``return` `gen.next(v); });   ``   ``}, ``function``(e) {``    ``step(``function``() { ``return` `gen.``throw``(e); });``   ``});``  ``}``  ``step(``function``() { ``return` `gen.next(undefined); });`` ``});``}`
</code></pre><p>async &#x51FD;&#x6570;&#x662F;&#x975E;&#x5E38;&#x65B0;&#x7684;&#x8BED;&#x6CD5;&#x529F;&#x80FD;&#xFF0C;&#x65B0;&#x5230;&#x90FD;&#x4E0D;&#x5C5E;&#x4E8E; ES6&#xFF0C;&#x800C;&#x662F;&#x5C5E;&#x4E8E; ES7&#x3002;&#x76EE;&#x524D;&#xFF0C;&#x5B83;&#x4ECD;&#x5904;&#x4E8E;&#x63D0;&#x6848;&#x9636;&#x6BB5;&#xFF0C;&#x4F46;&#x662F;&#x8F6C;&#x7801;&#x5668; Babel &#x548C; regenerator &#x90FD;&#x5DF2;&#x7ECF;&#x652F;&#x6301;&#xFF0C;&#x8F6C;&#x7801;&#x540E;&#x5C31;&#x80FD;&#x4F7F;&#x7528;&#x3002;</p>
<h2 id="&#x4E94;&#x3001;async-&#x51FD;&#x6570;&#x7684;&#x7528;&#x6CD5;"><strong>&#x4E94;&#x3001;async &#x51FD;&#x6570;&#x7684;&#x7528;&#x6CD5;</strong></h2>
<p>&#x540C; Generator &#x51FD;&#x6570;&#x4E00;&#x6837;&#xFF0C;async &#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; Promise &#x5BF9;&#x8C61;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; then &#x65B9;&#x6CD5;&#x6DFB;&#x52A0;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;&#x5F53;&#x51FD;&#x6570;&#x6267;&#x884C;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E00;&#x65E6;&#x9047;&#x5230; await &#x5C31;&#x4F1A;&#x5148;&#x8FD4;&#x56DE;&#xFF0C;&#x7B49;&#x5230;&#x89E6;&#x53D1;&#x7684;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x5B8C;&#x6210;&#xFF0C;&#x518D;&#x63A5;&#x7740;&#x6267;&#x884C;&#x51FD;&#x6570;&#x4F53;&#x5185;&#x540E;&#x9762;&#x7684;&#x8BED;&#x53E5;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x662F;&#x4E00;&#x4E2A;&#x4F8B;&#x5B50;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`async ``function` `getStockPriceByName(name) {`` ``var` `symbol = await getStockSymbol(name);`` ``var` `stockPrice = await getStockPrice(symbol);`` ``return` `stockPrice;``}``getStockPriceByName(``&apos;goog&apos;``).then(``function` `(result){`` ``console.log(result);``});`
</code></pre><p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x662F;&#x4E00;&#x4E2A;&#x83B7;&#x53D6;&#x80A1;&#x7968;&#x62A5;&#x4EF7;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x51FD;&#x6570;&#x524D;&#x9762;&#x7684;async&#x5173;&#x952E;&#x5B57;&#xFF0C;&#x8868;&#x660E;&#x8BE5;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x6709;&#x5F02;&#x6B65;&#x64CD;&#x4F5C;&#x3002;&#x8C03;&#x7528;&#x8BE5;&#x51FD;&#x6570;&#x65F6;&#xFF0C;&#x4F1A;&#x7ACB;&#x5373;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;Promise&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6307;&#x5B9A;&#x591A;&#x5C11;&#x6BEB;&#x79D2;&#x540E;&#x8F93;&#x51FA;&#x4E00;&#x4E2A;&#x503C;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`function` `timeout(ms) {`` ``return` `new` `Promise((resolve) =&gt; {``  ``setTimeout(resolve, ms);`` ``});``}``async ``function` `asyncPrint(value, ms) {`` ``await timeout(ms);`` ``console.log(value)``}``asyncPrint(``&apos;hello world&apos;``, 50);`
</code></pre><p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x6307;&#x5B9A;50&#x6BEB;&#x79D2;&#x4EE5;&#x540E;&#xFF0C;&#x8F93;&#x51FA;&quot;hello world&quot;&#x3002;</p>
<h2 id="&#x516D;&#x3001;&#x6CE8;&#x610F;&#x70B9;"><strong>&#x516D;&#x3001;&#x6CE8;&#x610F;&#x70B9;</strong></h2>
<p>await &#x547D;&#x4EE4;&#x540E;&#x9762;&#x7684; Promise &#x5BF9;&#x8C61;&#xFF0C;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x53EF;&#x80FD;&#x662F; rejected&#xFF0C;&#x6240;&#x4EE5;&#x6700;&#x597D;&#x628A; await &#x547D;&#x4EE4;&#x653E;&#x5728; try...catch &#x4EE3;&#x7801;&#x5757;&#x4E2D;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`async ``function` `myFunction() {`` ``try` `{``  ``await somethingThatReturnsAPromise();`` ``} ``catch` `(err) {``  ``console.log(err);`` ``}``}``// &#x53E6;&#x4E00;&#x79CD;&#x5199;&#x6CD5;``async ``function` `myFunction() {`` ``await somethingThatReturnsAPromise().``catch``(``function` `(err){``  ``console.log(err);`` ``});``}`
</code></pre><p>await &#x547D;&#x4EE4;&#x53EA;&#x80FD;&#x7528;&#x5728; async &#x51FD;&#x6570;&#x4E4B;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x7528;&#x5728;&#x666E;&#x901A;&#x51FD;&#x6570;&#xFF0C;&#x5C31;&#x4F1A;&#x62A5;&#x9519;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`async ``function` `dbFuc(db) {`` ``let docs = [{}, {}, {}];`` ``// &#x62A5;&#x9519;`` ``docs.forEach(``function` `(doc) {``  ``await db.post(doc);`` ``});``}`
</code></pre><p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x56E0;&#x4E3A; await &#x7528;&#x5728;&#x666E;&#x901A;&#x51FD;&#x6570;&#x4E4B;&#x4E2D;&#x4E86;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x5C06; forEach &#x65B9;&#x6CD5;&#x7684;&#x53C2;&#x6570;&#x6539;&#x6210; async &#x51FD;&#x6570;&#xFF0C;&#x4E5F;&#x6709;&#x95EE;&#x9898;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`async ``function` `dbFuc(db) {`` ``let docs = [{}, {}, {}];` ` ``// &#x53EF;&#x80FD;&#x5F97;&#x5230;&#x9519;&#x8BEF;&#x7ED3;&#x679C;`` ``docs.forEach(async ``function` `(doc) {``  ``await db.post(doc);`` ``});``}`
</code></pre><p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x53EF;&#x80FD;&#x4E0D;&#x4F1A;&#x6B63;&#x5E38;&#x5DE5;&#x4F5C;&#xFF0C;&#x539F;&#x56E0;&#x662F;&#x8FD9;&#x65F6;&#x4E09;&#x4E2A; db.post &#x64CD;&#x4F5C;&#x5C06;&#x662F;&#x5E76;&#x53D1;&#x6267;&#x884C;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x540C;&#x65F6;&#x6267;&#x884C;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x7EE7;&#x53D1;&#x6267;&#x884C;&#x3002;&#x6B63;&#x786E;&#x7684;&#x5199;&#x6CD5;&#x662F;&#x91C7;&#x7528; for &#x5FAA;&#x73AF;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`async ``function` `dbFuc(db) {`` ``let docs = [{}, {}, {}];`` ``for` `(let doc of docs) {``  ``await db.post(doc);`` ``}``}`
</code></pre><p>&#x5982;&#x679C;&#x786E;&#x5B9E;&#x5E0C;&#x671B;&#x591A;&#x4E2A;&#x8BF7;&#x6C42;&#x5E76;&#x53D1;&#x6267;&#x884C;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; Promise.all &#x65B9;&#x6CD5;&#x3002;</p>
<p><a href="https://www.jb51.net/article/140002.htm#" target="_blank">?</a></p>
<pre><code>`async ``function` `dbFuc(db) {`` ``let docs = [{}, {}, {}];`` ``let promises = docs.map((doc) =&gt; db.post(doc));`` ``let results = await Promise.all(promises);`` ``console.log(results);``}``// &#x6216;&#x8005;&#x4F7F;&#x7528;&#x4E0B;&#x9762;&#x7684;&#x5199;&#x6CD5;``async ``function` `dbFuc(db) {`` ``let docs = [{}, {}, {}];`` ``let promises = docs.map((doc) =&gt; db.post(doc));`` ``let results = [];`` ``for` `(let promise of promises) {``  ``results.push(await promise);`` ``}`` ``console.log(results);``}`
</code></pre><p><strong>&#x603B;&#x7ED3;</strong></p>
<p>&#x4EE5;&#x4E0A;&#x6240;&#x8FF0;&#x662F;&#x5C0F;&#x7F16;&#x7ED9;&#x5927;&#x5BB6;&#x4ECB;&#x7ECD;&#x7684;js &#x4E2D;async &#x51FD;&#x6570;&#x7684;&#x542B;&#x4E49;&#x548C;&#x7528;&#x6CD5;&#xFF0C;&#x5E0C;&#x671B;&#x5BF9;&#x5927;&#x5BB6;&#x6709;&#x6240;&#x5E2E;&#x52A9;&#xFF0C;&#x5982;&#x679C;&#x5927;&#x5BB6;&#x6709;&#x4EFB;&#x4F55;&#x7591;&#x95EE;&#x6B22;&#x8FCE;&#x7ED9;&#x6211;&#x7559;&#x8A00;&#xFF0C;&#x5C0F;&#x7F16;&#x4F1A;&#x53CA;&#x65F6;&#x56DE;&#x590D;&#x5927;&#x5BB6;&#x7684;&#xFF01;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Array.prototype.slice.call.html" class="navigation navigation-prev " aria-label="Previous page: Array.prototype.slice.call解析">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="callback&&callback如果存在回调函数就执行.html" class="navigation navigation-next " aria-label="Next page: callback&&callback() 如果存在回调函数就执行！">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"async 函数","level":"1.4","depth":1,"next":{"title":"callback&&callback() 如果存在回调函数就执行！","level":"1.5","depth":1,"path":"project/callback&&callback如果存在回调函数就执行.md","ref":"project/callback&&callback如果存在回调函数就执行.md","articles":[]},"previous":{"title":"Array.prototype.slice.call解析","level":"1.3","depth":1,"path":"project/Array.prototype.slice.call.md","ref":"project/Array.prototype.slice.call.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"project/async 函数.md","mtime":"2019-06-12T14:15:42.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-07-18T03:07:23.077Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

